package com.cam.generator.dto;

import com.cam.framework.crud.CrudDTO;
import com.cam.framework.system.common.dict.SysYesNoDict;
import com.cam.framework.utils.StringUtils;
import com.cam.generator.dict.GenCodeDbFieldTypeDict;
import com.cam.generator.dict.GenCodeFieldTypeDict;
import lombok.Data;

import java.util.Arrays;

@Data
public class GenCodeFieldDTO extends CrudDTO {

    private static final long serialVersionUID = 1L;

    private GenCodeDTO genCode;

    /*
    * java字段名
    * */
    private String javaField;

    /*
     * 字段注释、中文名
     * */
    private String label;

    /*
     * 字段类型，枚举类GenCodeFieldTypeDict
     * */
    private String type;

    /*
     * 数据库字段名
     * */
    private String dbField;

    /*
     * 数据库字段类型
     * */
    private String dbType;

    /*
     * 数据库字段长度
     * */
    private Integer dbLength;

    /*
     * 数据库字段小数点长度
     * */
    private Integer dbPrecision;

    /*
     * 数据库字段是否不为Null
     * */
    private String dbNotNullFlag;

    /*
     * 查询标识
     * */
    private String searchFlag;

    /*
     * 查询数据库字段
     * */
//    private String searchDbField;


    private String remarks;

    public GenCodeFieldTypeDict getTypeDict() {
        for (GenCodeFieldTypeDict eachDict : GenCodeFieldTypeDict.values()) {
            if (eachDict.getValue().equals(this.type)) {
                return eachDict;
            }
        }
        return null;
    }

    public GenCodeDbFieldTypeDict getDbTypeDict() {
        for (GenCodeDbFieldTypeDict eachDict : GenCodeDbFieldTypeDict.values()) {
            if (eachDict.getValue().equals(this.dbType)) {
                return eachDict;
            }
        }
        return null;
    }

    public SysYesNoDict getDbNotNullDict() {
        for (SysYesNoDict eachDict : SysYesNoDict.values()) {
            if (eachDict.getValue().equals(this.dbNotNullFlag)) return eachDict;
        }
        return null;
    }

    public SysYesNoDict getSearchDict() {
        for (SysYesNoDict eachDict : SysYesNoDict.values()) {
            if (eachDict.getValue().equals(this.searchFlag)) return eachDict;
        }
        return null;
    }

    public Boolean isDbFieldTextType() {
        GenCodeDbFieldTypeDict dbTypeDict = this.getDbTypeDict();
        if (dbTypeDict != null) {
            String[] textTypeArr = {"varchar", "text", "longtext"};
            return Arrays.asList(textTypeArr).contains(dbTypeDict.getDbType());
        }
        return false;

    }

    /*
     * 获取驼峰式字段名
     * */
    public String getCamelName() {
        return StringUtils.underlineToCamelCase(this.javaField);
    }

    /*
    * 获取首字母大写的java字段
    * */
    public String getPascalName() {
        return StringUtils.capitalize(this.javaField);
    }

}
